/*
 * @describe 日志Action.java
 * @author yisin
 * @date 2014-12-21 00:44:30
 */
package com.yisin.pbox.www.logs.action;

import java.util.List;

import com.yisin.pbox.bean.pojo.common.Datas;
import com.yisin.pbox.bean.pojo.system.Logs;
import com.yisin.pbox.frame.common.GlobalConstants;
import com.yisin.pbox.frame.common.Response;
import com.yisin.pbox.frame.common.SpringContainer;
import com.yisin.pbox.frame.common.StatusCode;
import com.yisin.pbox.frame.common.reflect.YXObject;
import com.yisin.pbox.frame.log.ILogger;
import com.yisin.pbox.frame.log.LogFactory;
import com.yisin.pbox.frame.util.StringUtil;
import com.yisin.pbox.www.common.datas.IDatasService;
import com.yisin.pbox.www.common.frame.BaseAction;
import com.yisin.pbox.www.logs.ILogsService;

/**
 * <pre>
 * 描述：日志Action
 * </pre>
 * 
 * @author yisin
 * @date 2014-12-21 00:44:30
 * 
 */
public class LogsAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static ILogger logger = LogFactory
			.getDefaultLogger(LogsAction.class);
	private ILogsService service = (ILogsService) SpringContainer
			.getBeanByName("logsService");
	private IDatasService dataService = (IDatasService) SpringContainer.getBeanByName("datasService");

	/**
	 * 页面跳转（新增/修改..页面）
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 * @see com.szkingdom.web.system.logs.action.LogsAction#pageJump
	 */
	public String pageJump() {
		String result = null;
		if (genre != null) {
			if (genre.equals(GlobalConstants.GENRE_ADD)) {// 请求跳转到新增页面
				result = verifyAction(GlobalConstants.NUM_2);
				if (result == null) {
					result = GlobalConstants.RETURN_PAGE;
					//
				}
			} else if (genre.equals(GlobalConstants.GENRE_EDIT)) {// 请求跳转到修改页面
				result = verifyAction(GlobalConstants.NUM_3);
				if (result == null) {
					result = GlobalConstants.RETURN_PAGE;
					// 注入属性值
					int logId = StringUtil.stringToInt(request
							.getParameter("logId"));
					Response res = service.getDataById(logId);
					if (res.getStatusCode() == StatusCode.SUCCESS) {
						Logs logs = (Logs) res.getObject();
						request.setAttribute("logs", logs);
					}
				}
			} else if (genre.equals(GlobalConstants.GENRE_AUDIT)) {// 请求跳转到审核页面
				result = verifyAction(GlobalConstants.NUM_6);
				if (result == null) {
					result = GlobalConstants.GENRE_AUDIT;
					// 注入属性值
					int logId = StringUtil.stringToInt(request
							.getParameter("logId"));
					Response res = service.getDataById(logId);
					if (res.getStatusCode() == StatusCode.SUCCESS) {
						Logs logs = (Logs) res.getObject();
						request.setAttribute("logs", logs);
					}
				}
			} else {
				result = "error404";
			}
		}
		return result;
	}

	/**
	 * 加载或搜索功能记录
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 * @see com.szkingdom.web.system.logs.action.LogsAction#loadAndSearch
	 */
	public void loadAndSearch() {
		logger.info("Into method#loadAndSearch");
		if (!verifyUser(GlobalConstants.NUM_1)) {
			return;// 没有权限，直接返回。
		}
		Logs logs = new Logs();
		// 填充参数
		YXObject.setValueForSearch(logs, request);
		Response res = service.loadAndSearch(logs);
		if (res.getStatusCode() == StatusCode.SUCCESS) {
			logger.info("Out method#loadAndSearch data");
			List list = res.getList();
			if (!isExport()) {
				Datas data = new Datas();
				data.setParamObject(logs);
				data.setMapper("LogsMapper.loadAndSearchCount");
				res = dataService.excsql(data);
				if (res.getStatusCode() == StatusCode.SUCCESS) {
					List<Logs> flist = (List<Logs>) res.getList();
					logs = flist.get(0);
				}
				outListToJson(list, logs.getSize(), "loadAndSearch", 1, null);
			} else {
				exportExcel(list);
			}
		} else {
			logger.info("Out method#loadAndSearch []");
			if (!isExport()) {
				outString("[]");
			} else {
				outExportError();
			}
		}
	}

	/**
	 * 高级查找
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 * @see com.szkingdom.web.system.logs.action.LogsAction#advancedSearch
	 */
	public void advancedSearch() {
		logger.info("Into method#advancedSearch");
		if (!verifyUser(GlobalConstants.NUM_1)) {
			return;// 没有权限，直接返回。
		}
		Logs logs = new Logs();
		// 填充参数
		YXObject.setValueForSearch(logs, request);


		Response res = service.advancedSearch(logs);
		if (res.getStatusCode() == StatusCode.SUCCESS) {
			List list = res.getList();
			if (!isExport()) {
				Datas data = new Datas();
				data.setParamObject(logs);
				data.setMapper("LogsMapper.advancedSearchCount");
				res = dataService.excsql(data);
				if (res.getStatusCode() == StatusCode.SUCCESS) {
					List<Logs> flist = (List<Logs>) res.getList();
					logs = flist.get(0);
				}
				outListToJson(list, logs.getSize(), "loadAndSearch", 1, null);
			} else {
				exportExcel(list);
			}
		} else {
			logger.info("Out method#advancedSearch []");
			if (!isExport()) {
				outString("[]");
			} else {
				outExportError();
			}
		}
	}
	
	/**
	 * 精确查询
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 * @see com.szkingdom.web.system.logs.action.LogsAction#selectDataForAccurate
	 */
	public void selectDataForAccurate(){
		logger.info("Into method#selectDataForAccurate");
		if (!verifyUser(GlobalConstants.NUM_1)) {
			return;// 没有权限，直接返回。
		}
		Logs logs = new Logs();
		// 填充参数
		YXObject.setValue(logs, request);
		Response res = service.selectDataForAccurate(logs);
		if (res.getStatusCode() == StatusCode.SUCCESS) {
			logger.info("Out method#selectDataForAccurate data");
			List list = res.getList();
			outListToJson(list, list.size(), "selectDataForAccurate", 1, null);
		} else {
			logger.info("Out method#selectDataForAccurate []");
			outString("[]");
		}
	}

	/**
	 * 增加日志
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 * @see com.szkingdom.web.system.logs.action.LogsAction#insertData
	 */
	public void insertData() {
		logger.info("Into method#insertData");
		if (!verifyUser(GlobalConstants.NUM_2)) {
			return;// 没有权限，直接返回。
		}
		Logs logs = new Logs();
		// 填充参数
		YXObject.setValue(logs, request);
		Response res = service.insertData(logs);
		if (res.getStatusCode() == StatusCode.SUCCESS) {
			outJson(GlobalConstants.GENRE_ADD, GlobalConstants.RETURN_SUCCESS);
		} else {
			outJson(GlobalConstants.GENRE_ADD, GlobalConstants.RETURN_FAILED);
		}
		// 插入操作日志
		operateLogger("新增日志[Logs]信息到表[logs]", logs);
	}

	/**
	 * 修改日志
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 * @see  com.szkingdom.web.system.logs.action.LogsAction#updateData
	 */
	public void updateData() {
		logger.info("Into method#updateData");
		if (!verifyUser(GlobalConstants.NUM_3)) {
			return;// 没有权限，直接返回。
		}
		Logs logs = new Logs();
		// 填充参数
		YXObject.setValue(logs, request);
		Response res = service.updateData(logs);
		if (res.getStatusCode() == StatusCode.SUCCESS) {
			outJson(GlobalConstants.GENRE_EDIT, GlobalConstants.RETURN_SUCCESS);
		} else {
			outJson(GlobalConstants.GENRE_EDIT, GlobalConstants.RETURN_FAILED);
		}
		// 插入操作日志
		operateLogger("修改日志[Logs]信息到表[logs]", logs);
	}

	/**
	 * 删除日志
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 * @see com.szkingdom.web.system.logs.action.LogsAction#deleteData
	 */
	public void deleteData() {
		logger.info("Into method#deleteData");
		if (!verifyUser(GlobalConstants.NUM_4)) {
			return;// 没有权限，直接返回。
		}
		Logs logs = new Logs();
		// 填充参数
		YXObject.setValue(logs, request);
		Response res = service.getDataById(logs.getLogId());
		if (res.getStatusCode() == StatusCode.SUCCESS) {
			logs = (Logs) res.getObject();
			res = service.deleteData(logs);
			if (res.getStatusCode() == StatusCode.SUCCESS) {
				outJson(GlobalConstants.GENRE_DELETE, GlobalConstants.RETURN_SUCCESS);
			} else {
				outJson(GlobalConstants.GENRE_DELETE, GlobalConstants.RETURN_FAILED);
			}
		} else {
			outJson(GlobalConstants.GENRE_DELETE, GlobalConstants.RETURN_FAILED);
		}
		// 插入操作日志
		operateLogger("删除日志[Logs]信息到表[logs]", logs);
	}
	
	/**
	 * 审核日志    （不需要的可以删除）
	 * 
	 * @author yisin
	 * @date 2014-12-21 00:44:30
	 * @see com.szkingdom.web.system.logs.action.LogsAction#auditingData
	 */
	public void auditingData() {
		logger.info("Into method#auditingData");
		if (!verifyUser(GlobalConstants.NUM_6)) {
			return;// 没有权限，直接返回。
		}
		Logs logs = new Logs();
		// 填充参数
		YXObject.setValue(logs, request);
		Response res = service.auditingData(logs);
		if (res.getStatusCode() == StatusCode.SUCCESS) {
			outJson(GlobalConstants.GENRE_AUDIT, GlobalConstants.RETURN_SUCCESS);
		} else {
			outJson(GlobalConstants.GENRE_AUDIT, GlobalConstants.RETURN_FAILED);
		}
		// 插入操作日志
		operateLogger("审核日志[Logs]信息到表[logs]", logs);
	}

}
